package com.njuptB180316.boki.controller;

import com.njuptB180316.boki.dao.BookMapper;
import com.njuptB180316.boki.dao.UserMapper;
import com.njuptB180316.boki.dataobject.Book;
import com.njuptB180316.boki.dataobject.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RegisterController {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 返回注册界面
     */
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    /**
     * 注册界面
     */
    @RequestMapping("/registerinfo")
    public String registerinfo(@RequestParam("phone") String phone,
                               @RequestParam("name") String name,
                               @RequestParam("password") String password,
                               ModelMap modelMap){
        User userTemp = userMapper.selectByPrimaryKey(Long.parseLong(phone));
        if (userTemp!=null){
            modelMap.addAttribute("msg", "手机号已存在，请勿重新注册");
            return "error";
        }else{
            User user = new User();
            user.setAuthority(0);
            user.setName(name);
            user.setPassword(password);
            user.setPhone(Long.parseLong(phone));
            modelMap.addAttribute("user",user);
            userMapper.insert(user);
            return "redirect:/";
        }
    }
}
